<?php
/*
+ --------------------------------------------------- +
|  Ineo Article Manager - Version 2.x
|  Copyright 2006-2007, Dan Cryer and William McGann
+ --------------------------------------------------- +

Ineo Article Manager is free software; you can redistribute it and/or modify
it under the terms of version 3 of the GNU General Public License as published by
the Free Software Foundation.

Ineo Article Manager is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Ineo; see the file License.txt.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

*/

/**
* Ineo - Modules - Admin CP Permissions
*
* @package		Ineo
* @subpackage   Modules
* @copyright	2006, Veriea
*/
class ineo_mod_acp_permissions
{
	/**
	* Variable Setup
	*/
	var $ipsclass;
	var $core;
	
	/**
	* Initialize
	*/
	function module_init()
	{
		// ACP Module, Do nothing if not in ACP!
		if( !IN_ACP )
		{
			return false;
		}
	}
	
	/**
	* Default Function for this module:
	*/
	function module_default()
	{
		return $this->permissions();
	}
	
	/**
	* Load and show permissions!
	*/
	function permissions()
	{
		$category = 'global';
		$this->ipsclass->admin->page_title = 'Ineo Global Permissions';
		$this->ipsclass->admin->page_detail = 'Here you can control the access rights of each member group.';
		
		if( !empty( $this->ipsclass->input['category']) )
		{
			$category = intval( $this->ipsclass->input['category'] );
			$cat = $this->ipsclass->cache['ineo_cats'][$category]['info'];
			$this->ipsclass->admin->page_title = 'Permissions for Category: ' . $cat['category_name'];
		}
		
		$rows = $this->get_group_perms($category);
		
		// JS crap:
		$this->ipsclass->html .= <<<SCRIPT
	<script type='text/javascript'>
	
	function check_all_group( group_id , new_value )
	{
		var cView    = document.getElementById('view_' + group_id );
		var cAdd     = document.getElementById('add_' + group_id );
		var cEdit    = document.getElementById('edit_' + group_id );
		var cQueue   = document.getElementById('queue_' + group_id );
		var cUpload  = document.getElementById('upload_' + group_id );
		var cIsMod   = document.getElementById('ismod_' + group_id );
		
		cView.checked = new_value;
		cAdd.checked  = new_value;
		cEdit.checked = new_value;
		cQueue.checked = new_value;
		cUpload.checked = new_value;
		cIsMod.checked = new_value;
	}	
	
	function check_all_type( type_id , new_value )
	{
		var frm = document.getElementById('permissions-form');
		var checks = frm.getElementsByTagName('input');
		
		for( var i = 0; i <= checks.length; i++ )
		{
			var box = checks[i];
			var bid = box.id;
			var typ = bid.replace( /^(.+?)_.+?$/, "$1" );
			
			if( typ == type_id )
			{
				box.checked = new_value;
			}
		}
	}
	
	function check_all( new_value )
	{
		var frm = document.getElementById('permissions-form');
		var checks = frm.getElementsByTagName('input');
		
		for( var i = 0; i <= checks.length; i++ )
		{
			var box = checks[i];
			box.checked = new_value;
		}
	}
	
	</script>
SCRIPT;
		
		$html = '<form id="permissions-form" action="'.$this->ipsclass->base_url.'&section=components&act=ineo&module=permissions&code=do_perms" method="post">
	<input type="hidden" name="permission_type" value="'.$category.'" />
		         <table style="border-spacing: 0px" width="100%" cellspacing="0" cellpadding="0" style="margin: 0px">
  					  '.$rows.'
            </table></form>';
		
		$this->core->api->box( 'Permissions', $html );
	}
	
	/**
	* Get group permissions
	*/
	function get_group_perms( $category = 'global' )
	{
		// Load the permissions cache:
		$this->perms =& $this->ipsclass->cache['ineo_permissions'][$category];
		
		if( !empty($this->ipsclass->input['msg']) )
		{
			// If we've just updated some permissions, lets make that known!
			$this->core->api->box('Permissions Updated','<table style="width: 100%"><td class="{$this->core->api->row2}">The permissions cache has been updated!</td></table>');
		}
		
		// Load all of the groups in the groups table:
		$getgroups = array('select' => 'g_id, g_title', 'from' => 'groups');
	  	$this->ipsclass->DB->simple_construct($getgroups);
    	$this->ipsclass->DB->simple_order( 'g_title ASC' );
    	$this->ipsclass->DB->simple_exec();
    	
		if( $category == 'global' )
		{
			$rows .= $this->permissions_row(array('g_id' => 0, 'g_title' => 'Global Default'));
		}
		
		// loop groups, show perms:
    	while ( $perm = $this->ipsclass->DB->fetch_row() )
    	{
      		$rows .= $this->permissions_row($perm);
    	}
    
		$rows .= <<<CHECKVERT
<tr>
	<td colspan='7' class='{$this->core->api->thead}'>
		Toggle All Groups
	</td>
</tr>

	<tr>
		<td class='{$this->core->api->row1}' style='text-align: center;'>
			<input style='width: 20px; height: 20px; background: #f5cdcd; border: 1px solid #777777; font-weight: bold;' type="button" id="button" value="+" onclick="javascript:check_all('checked')" />&nbsp;
			<input style='width: 20px; height: 20px; background: #f5cdcd; border: 1px solid #777777; font-weight: bold;' type="button" id="button" value="-" onclick="javascript:check_all('')" />
		</td>
		<td class="{$this->core->api->row1}" valign="middle" style="text-align: center; padding: 5px">
			<input style='width: 20px; height: 20px;' type="button" id="button" value="+" onclick="javascript:check_all_type('view','checked')" />&nbsp;
			<input style='width: 20px; height: 20px;' type="button" id="button" value="-" onclick="javascript:check_all_type('view','')" />
		</td>
		<td class="{$this->core->api->row1}" valign="middle" style="text-align: center; padding: 5px">
			<input style='width: 20px; height: 20px;' type="button" id="button" value="+" onclick="javascript:check_all_type('add','checked')" />&nbsp;
			<input style='width: 20px; height: 20px;' type="button" id="button" value="-" onclick="javascript:check_all_type('add','')" />
		</td>
		<td class="{$this->core->api->row1}" valign="middle" style="text-align: center; padding: 5px">
			<input style='width: 20px; height: 20px;' type="button" id="button" value="+" onclick="javascript:check_all_type('edit','checked')" />&nbsp;
			<input style='width: 20px; height: 20px;' type="button" id="button" value="-" onclick="javascript:check_all_type('edit','')" />
		</td>
		<td class="{$this->core->api->row1}" valign="middle" style="text-align: center; padding: 5px">
			<input style='width: 20px; height: 20px;' type="button" id="button" value="+" onclick="javascript:check_all_type('queue','checked')" />&nbsp;
			<input style='width: 20px; height: 20px;' type="button" id="button" value="-" onclick="javascript:check_all_type('queue','')" />
		</td>
		<td class="{$this->core->api->row1}" valign="middle" style="text-align: center; padding: 5px">
			<input style='width: 20px; height: 20px;' type="button" id="button" value="+" onclick="javascript:check_all_type('upload','checked')" />&nbsp;
			<input style='width: 20px; height: 20px;' type="button" id="button" value="-" onclick="javascript:check_all_type('upload','')" />
		</td>
		<td class="{$this->core->api->row1}" valign="middle" style="text-align: center; padding: 5px">
			<input style='width: 20px; height: 20px;' type="button" id="button" value="+" onclick="javascript:check_all_type('ismod','checked')" />&nbsp;
			<input style='width: 20px; height: 20px;' type="button" id="button" value="-" onclick="javascript:check_all_type('ismod','')" />
		</td>
	</tr>
CHECKVERT;

    	$rows .= '<tr><td class="'.$this->core->api->tfoot.'" colspan="8" align="center"><input value="Update Group Permissions" id="button" accesskey="s" type="submit"></td></tr>';
    
    	return $rows;
	}
	
	function permissions_row( $perm )
	{
		$gid    = $perm['g_id'];
  		$title  = $perm['g_title'];
  		$view   = ( $this->perms[$gid]['view']   != '1' ) ? '' : 'checked="checked" ';
  		$add    = ( $this->perms[$gid]['add']    != '1' ) ? '' : 'checked="checked" ';
 		$edit   = ( $this->perms[$gid]['edit']   != '1' ) ? '' : 'checked="checked" ';
  		$queue  = ( $this->perms[$gid]['avoid_queue']  != '1' ) ? '' : 'checked="checked" ';
  		$upload = ( $this->perms[$gid]['upload'] != '1' ) ? '' : 'checked="checked" ';
  		$ismod  = ( $this->perms[$gid]['moderate']  != '1' ) ? '' : 'checked="checked" ';
		
  		return '
		<tr>
			<td colspan="7" class='.$this->core->api->thead.'>
				'.$title.'
			</td>
		</tr>
<tr>
			<td class="'.$this->core->api->row1.'" valign="middle" style="padding: 0; text-align: center;">
				<input style="width: 20px; height: 20px" type="button" id="button" value="+" onclick="javascript:check_all_group('.$gid.',\'checked\')" />&nbsp;
				<input style="width: 20px; height: 20px" type="button" id="button" value="-" onclick="javascript:check_all_group('.$gid.',\'\')" />
			</td>
			
            <td valign="middle" style="background-color:#dbe6ea;" class="'.$this->core->api->row2.'">
				<div id="mgyellow" class="yellow-perm" style="text-align: center; padding: 5px;">
					View <input id="view_'.$gid.'" name="view_'.$gid.'" value="1" '.$view.'type="checkbox">
				</div>
			</td>
			<td valign="middle" style="background-color:#d2d5f2" class="'.$this->core->api->row2.'">
				<div id="mgblue" class="blue-perm" style="text-align: center; padding: 5px">
					Add <input id="add_'.$gid.'" name="add_'.$gid.'" value="1" '.$add.'type="checkbox">
				</div>
			</td>
			<td valign="middle" style="background-color:#dbe2de" class="'.$this->core->api->row2.'">
				<div id="mggreen" class="green-perm" style="text-align: center; padding: 5px">
					Edit <input id="edit_'.$gid.'" name="edit_'.$gid.'" value="1" '.$edit.'type="checkbox">
				</div>
			</td>
			<td valign="middle" style="background-color:#ecd5d8" class="'.$this->core->api->row2.'">
				<div id="mgred" class="red-perm" style="text-align: center; padding: 5px;">
					Avoid Queue <input id="queue_'.$gid.'" name="queue_'.$gid.'" value="1" '.$queue.'type="checkbox">
				</div>
			</td>
			<td valign="middle" style="background-color:#ece6d8" class="'.$this->core->api->row2.'">
				<div id="memgroup" class="orange-perm" style="text-align: center; padding: 5px">
					Upload <input id="upload_'.$gid.'" name="upload_'.$gid.'" value="1" '.$upload.'type="checkbox">
				</div>
			</td>
			<td valign="middle" style="background-color:#dfdee9" class="'.$this->core->api->row2.'">
				<div id="mgyellow" class="purple-perm" style="text-align: center; padding: 5px;">
					Moderate <input id="ismod_'.$gid.'" name="ismod_'.$gid.'" value="1" '.$ismod.'type="checkbox">
				</div>
			</td>';
	}
	
	/**
	* Get incoming permissions, update the database with new values:
	*/
	function do_perms()
	{
		// Load groups first:
		$this->ipsclass->DB->simple_construct( array( 'select' => 'g_id', 'from' => 'groups' ) );
		$this->ipsclass->DB->simple_order( 'g_id ASC' );
		$loop = $this->ipsclass->DB->simple_exec();
	  
		$category = $this->ipsclass->input['permission_type'];
		
		if( !empty( $this->ipsclass->input['view_0'] ) )
		{
			// Set global default if we're doing global perms:
			$this->set_permissions( $category, 0 );
		}
		
		// loop through all of the groups, if permissions are set for it, update the db. If not, don't. Simple as.
	  	while ( $group = $this->ipsclass->DB->fetch_row($loop) )
	  	{
	    	$this->set_permissions( $category, $group['g_id'] );
	  	}
	  	
		if( $category == 'global' )
		{
			$actions = '&section=components&act=ineo&module=permissions&msg=1';
			$this->ipsclass->boink_it($this->ipsclass->base_url.$actions);
		}
		else
		{
			$actions = '&section=components&act=ineo&module=permissions&category='.$category.'&msg=1';
			$this->ipsclass->boink_it($this->ipsclass->base_url.$actions);
		}	
	}
	
	function set_permissions( $category, $group )
	{
		$view   = ( isset($this->ipsclass->input['view_' . $group]  ) ) ? '1' : '-1';
    	$add    = ( isset($this->ipsclass->input['add_' . $group]   ) ) ? '1' : '-1';
    	$edit   = ( isset($this->ipsclass->input['edit_' . $group]  ) ) ? '1' : '-1';
    	$queue  = ( isset($this->ipsclass->input['queue_' . $group] ) ) ? '1' : '-1';
    	$upload = ( isset($this->ipsclass->input['upload_' . $group]) ) ? '1' : '-1';
    	$ismod  = ( isset($this->ipsclass->input['ismod_' . $group] ) ) ? '1' : '-1';
    
		$permissions = array( 'view' => $view , 'add' => $add , 'edit' => $edit, 'avoid_queue' => $queue, 'upload' => $upload, 'moderate' => $ismod );
						
		$this->ipsclass->cache['ineo_permissions'][$category][$group] = $permissions;
		
		$this->ipsclass->update_cache( array( 'name' => 'ineo_permissions', 'array' => 1, 'deletefirst' => 1 ) );
	}
}

?>